package cn.tedu.ivos.geofence.service.impl;

import cn.tedu.ivos.base.exception.ServiceException;
import cn.tedu.ivos.base.response.StatusCode;
import cn.tedu.ivos.geofence.mapper.GeofenceMapper;
import cn.tedu.ivos.geofence.pojo.dto.GeofenceParam;
import cn.tedu.ivos.geofence.pojo.dto.GeofenceQuery;
import cn.tedu.ivos.geofence.pojo.entity.Geofence;
import cn.tedu.ivos.geofence.pojo.vo.GeofenceVO;
import cn.tedu.ivos.geofence.service.GeofenceService;
import cn.tedu.ivos.vehicle.mapper.VehicleMapper;
import cn.tedu.ivos.vehicle.pojo.dto.VehicleQuery;
import cn.tedu.ivos.vehicle.pojo.entity.Vehicle;
import cn.tedu.ivos.vehicle.pojo.vo.VehicleVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

@Slf4j
@Service
public class GeofenceImpl implements GeofenceService {
    @Autowired
    GeofenceMapper geofenceMapper;
    @Override
    public List<GeofenceVO> select(GeofenceQuery geofenceQuery) {
        log.debug("查询电子围栏业务 : geofenceQuery={}",geofenceQuery);
        List<GeofenceVO> geofenceVOS = geofenceMapper.select(geofenceQuery);
        return geofenceVOS;
    }

    @Override
    public void save(GeofenceParam geofenceParam) {
        log.debug("保存电子围栏业务 : geofenceParam={}",geofenceParam);
        Geofence geofence = new Geofence();
        BeanUtils.copyProperties(geofenceParam,geofence);
        if (geofence.getId() != null){
         geofence.setUpdateTime(new Date());
         geofenceMapper.update(geofence);
        }else{
            geofence.setStatus("1");
            geofence.setCreateTime(new Date());
            geofenceMapper.insert(geofence);
        }
    }

    @Autowired
    VehicleMapper vehicleMapper;
    @Override
    public void delete(Long id) {
        log.debug("删除电子围栏业务 : id={}",id);
        VehicleQuery vehicleQuery = new VehicleQuery();
        vehicleQuery.setGeofenceId(id);
        List<VehicleVO> vehicleVOS = vehicleMapper.select(vehicleQuery);
        if (vehicleVOS.size()>0 && vehicleVOS != null){
            throw new ServiceException(StatusCode.OPERATION_FAILED);
        }else{
            geofenceMapper.deleteById(id);
        }
    }
}
